package cn.edu.xcu.ckl.controller;

import cn.edu.xcu.ckl.api.ApiConstants;
import cn.edu.xcu.ckl.domain.*;
import cn.edu.xcu.ckl.http.HttpResponse;
import cn.edu.xcu.ckl.http.StatusCode;
import cn.edu.xcu.ckl.service.UserInfoService;
import cn.edu.xcu.ckl.utils.DateUtils;
import cn.edu.xcu.ckl.utils.GsonUtils;
import cn.edu.xcu.ckl.utils.ImageFileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping(value = "user")
public class UserIController {

    @Autowired
    UserInfoService userInfoService;

    /**
     * 注册
     */
    @RequestMapping(value = "register", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<UserInfo> register(UserInfo userInfo) {

        HttpResponse<UserInfo> httpResponse = new HttpResponse<UserInfo>(StatusCode.Success);

        userInfo.setCreate_time(DateUtils.getCurrentTime());
        UserInfo register = userInfoService.login(userInfo);
        if (register == null) {
            int row = userInfoService.register(userInfo);
            if (row > 0) {
                httpResponse.setMsg("注册成功");
                httpResponse.setCode(StatusCode.Success.getCode());
            } else {
                httpResponse.setMsg("注册失败");
                httpResponse.setCode(StatusCode.Fail.getCode());
            }
        } else {
            httpResponse = new HttpResponse<UserInfo>(StatusCode.Fail);
            httpResponse.setMsg("该账号已注册");
        }
        httpResponse.setData(userInfo);
        return httpResponse;

    }


    /**
     * 登录
     */

    @RequestMapping(value = "login", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<UserInfo> login(UserInfo userInfo) {
        HttpResponse<UserInfo> httpResponse = new HttpResponse<UserInfo>(StatusCode.Success);
        UserInfo user = userInfoService.login(userInfo);
        if (null != user) {
            if (!user.getPassword().equals(userInfo.getPassword())) {
                httpResponse = new HttpResponse<UserInfo>(StatusCode.Fail);
                httpResponse.setMsg("密码错误");
            } else if (userInfo.getRegister_type() != user.getRegister_type()) {
                httpResponse = new HttpResponse<UserInfo>(StatusCode.Fail);
                httpResponse.setMsg("账号类型登录错误");
            } else {
                httpResponse = new HttpResponse<UserInfo>(StatusCode.Success);
                httpResponse.setMsg("登录成功");
                httpResponse.setData(user);
            }

        } else {
            httpResponse = new HttpResponse<UserInfo>(StatusCode.Fail);
            httpResponse.setMsg("该账号暂未注册");
        }
        return httpResponse;

    }


    /**
     * 修改密码/用户编辑
     */

    @RequestMapping(value = "edit", method = RequestMethod.POST)
    @ResponseBody
    public HttpResponse<UserInfo> edit(UserInfo userInfo, MultipartFile file) {
        HttpResponse<UserInfo> httpResponse = new HttpResponse<UserInfo>(StatusCode.Success);
        if (file != null) {
            userInfo.setAvatar(ApiConstants.BASE_URL + ImageFileUtils.loadImg(file));
        }
        int row = userInfoService.pwd(userInfo);
        if (row > 0) {
            httpResponse.setMsg("修改成功");
        } else {
            httpResponse.setMsg("修改失败");
        }
        httpResponse.setData(userInfo);
        return httpResponse;

    }


    /***
     * 上传头像的方法  uploader
     */
    @RequestMapping(value = "uploader", method = RequestMethod.POST)
    @ResponseBody
    public HttpResponse<UserInfo> uploader(MultipartFile file) {
        HttpResponse<UserInfo> httpResponse = new HttpResponse<UserInfo>(StatusCode.Success);
        UserInfo userInfo = new UserInfo();
        if (null != file) {
            String img = ImageFileUtils.loadImg(file);
            if (img != null) {
                userInfo.setAvatar(ApiConstants.BASE_URL + img);
            } else {
                httpResponse = new HttpResponse<UserInfo>(StatusCode.Fail);
            }
        } else {
            httpResponse = new HttpResponse<UserInfo>(StatusCode.Fail);
        }
        httpResponse.setData(userInfo);
        return httpResponse;
    }

    /**
     * 上架商品
     */
    @RequestMapping(value = "addProduct", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<ProductInfo> addProduct(ProductInfo productInfo) {
        HttpResponse<ProductInfo> httpResponse = new HttpResponse<>(StatusCode.Success);
        productInfo.setProduct_id(DateUtils.getRandom(10));
        int row = userInfoService.addProduct(productInfo);
        if (row > 0) {
            httpResponse.setMsg("上架成功");
        } else {
            httpResponse = new HttpResponse<>(StatusCode.Fail);
            httpResponse.setMsg("上架失败");
        }
        httpResponse.setData(productInfo);
        return httpResponse;

    }

    /**
     * 获取商品列表
     */
    @RequestMapping(value = "queryProductList", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<ProductListInfo> queryProductList(String product_type) {
        HttpResponse<ProductListInfo> httpResponse = new HttpResponse<>(StatusCode.Success);
        httpResponse.setData(new ProductListInfo(userInfoService.queryProductList(product_type)));
        return httpResponse;
    }

    /**
     * 删除商品
     */
    @RequestMapping(value = "delProduct", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<ProductInfo> delProduct(int _id) {
        HttpResponse<ProductInfo> httpResponse = new HttpResponse<>(StatusCode.Success);

        int row = userInfoService.delProduct(_id);
        if (row > 0) {
            httpResponse.setMsg("删除成功");
        } else {
            httpResponse.setMsg("删除失败");
        }
        httpResponse.setData(new ProductInfo());
        return httpResponse;

    }

    /**
     * 编辑商品
     */

    @RequestMapping(value = "editProduct", method = RequestMethod.POST)
    @ResponseBody
    public HttpResponse<ProductInfo> editProduct(ProductInfo productInfo, int _id) {
        HttpResponse<ProductInfo> httpResponse = new HttpResponse<>(StatusCode.Success);
        productInfo.set_id(_id);
        int row = userInfoService.editProduct(productInfo);
        if (row > 0) {
            httpResponse.setMsg("修改成功");
        } else {
            httpResponse = new HttpResponse<>(StatusCode.Fail);
            httpResponse.setMsg("修改失败");
        }
        httpResponse.setData(productInfo);
        return httpResponse;

    }


    /**
     * 添加购物车
     */
    @RequestMapping(value = "addCar", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<CarInfo> addCar(CarInfo carInfo) {
        HttpResponse<CarInfo> httpResponse = new HttpResponse<>(StatusCode.Success);
        CarInfo isAddCarInfo = userInfoService.isAddCar(carInfo);
        if (isAddCarInfo == null) {
            int row = userInfoService.addCar(carInfo);
            if (row > 0) {
                httpResponse.setMsg("添加成功");
            } else {
                httpResponse.setMsg("添加失败");
            }
        } else {
            isAddCarInfo.setProduct_count(isAddCarInfo.getProduct_count() + 1);
            int row = userInfoService.updateCar(isAddCarInfo);
            if (row > 0) {
                httpResponse.setMsg("添加成功");
            } else {
                httpResponse.setMsg("添加失败");
            }

            carInfo.setProduct_count(isAddCarInfo.getProduct_count() + 1);
        }

        httpResponse.setData(carInfo);
        return httpResponse;

    }


    /**
     * 减购
     */

    @RequestMapping(value = "subTractCar", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<CarInfo> subTractCar(CarInfo carInfo) {
        HttpResponse<CarInfo> httpResponse = new HttpResponse<>(StatusCode.Success);
        CarInfo isAddCarInfo = userInfoService.isAddCar(carInfo);
        if (isAddCarInfo.getProduct_count() > 1) {
            isAddCarInfo.setProduct_count(isAddCarInfo.getProduct_count() - 1);
            int row = userInfoService.updateCar(isAddCarInfo);
            if (row > 0) {
                httpResponse.setMsg("添加成功");
            } else {
                httpResponse = new HttpResponse<>(StatusCode.Fail);
                httpResponse.setMsg("添加失败");
            }
        } else {
            httpResponse = new HttpResponse<>(StatusCode.Fail);
            httpResponse.setMsg("商品购买数量1份起步，不能再减购啦～");
        }
        httpResponse.setData(isAddCarInfo);
        return httpResponse;

    }


    /**
     * 根据用户名查询购物车
     */
    @RequestMapping(value = "queryCarByUsername", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<CarListInfo> queryCarByUsername(String username) {
        HttpResponse<CarListInfo> httpResponse = new HttpResponse<>(StatusCode.Success);
        httpResponse.setData(new CarListInfo(userInfoService.queryCarByUsername(username)));
        return httpResponse;

    }

    /**
     * 删除购物车
     */
    @RequestMapping(value = "delCarById", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<CarInfo> delCarById(int car_id) {
        HttpResponse<CarInfo> httpResponse = new HttpResponse<>(StatusCode.Success);

        int row = userInfoService.delCarById(car_id);
        if (row > 0) {
            httpResponse.setMsg("删除成功");
        } else {
            httpResponse.setMsg("删除失败");
        }
        httpResponse.setData(new CarInfo());
        return httpResponse;

    }

    /**
     * 批量删除购物车
     */
    @RequestMapping(value = "clearCarByUsername", method = RequestMethod.POST)
    @ResponseBody
    public HttpResponse<CarListInfo> clearCarByUsername(String json) {
        HttpResponse<CarListInfo> httpResponse = new HttpResponse<>(StatusCode.Success);

        CarListInfo carListInfo = GsonUtils.parseJson(json, CarListInfo.class);

        int row = userInfoService.clearCarByUsername(carListInfo.getList());
        if (row > 0) {
            httpResponse.setMsg("删除成功");
        } else {
            httpResponse.setMsg("删除失败");
        }
        httpResponse.setData(carListInfo);
        return httpResponse;

    }

    /**
     * 创建订单
     */
    @RequestMapping(value = "createOrder", method = RequestMethod.POST)
    @ResponseBody
    public HttpResponse<OrderListInfo> createOrder(String json) {
        HttpResponse<OrderListInfo> httpResponse = new HttpResponse<>(StatusCode.Success);
        if (json != null) {
            OrderListInfo orderListInfo = GsonUtils.parseJson(json, OrderListInfo.class);
            int row = userInfoService.createOrder(orderListInfo.getList());
            if (row > 0) {
                httpResponse.setData(new OrderListInfo());
                httpResponse.setMsg("支付成功");
            } else {
                httpResponse = new HttpResponse<>(StatusCode.Fail);
                httpResponse.setData(new OrderListInfo());
                httpResponse.setMsg("支付失败");
            }
        } else {
            httpResponse = new HttpResponse<>(StatusCode.Fail);
            httpResponse.setData(new OrderListInfo());
        }
        return httpResponse;
    }

    /**
     * 查询用户订单
     */
    @RequestMapping(value = "queryOrderByUsername", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<OrderListInfo> queryOrderByUsername(String username) {
        HttpResponse<OrderListInfo> httpResponse = new HttpResponse<>(StatusCode.Success);
        httpResponse.setData(new OrderListInfo(userInfoService.queryOrderByUsername(username)));
        return httpResponse;

    }

    /**
     * 删除订单
     */
    @RequestMapping(value = "delOrderById", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<OrderInfo> delOrderById(int order_id) {
        HttpResponse<OrderInfo> httpResponse = new HttpResponse<>(StatusCode.Success);
        int row = userInfoService.delOrderById(order_id);
        if (row > 0) {
            httpResponse.setMsg("删除成功");
        } else {
            httpResponse.setMsg("删除失败");
        }
        httpResponse.setData(new OrderInfo(order_id));
        return httpResponse;

    }

    /**
     * 添加商品分类
     */
    @RequestMapping(value = "addProductType", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<ProductInfo> addProductType(ProductInfo productInfo) {
        HttpResponse<ProductInfo> httpResponse = new HttpResponse<>(StatusCode.Success);

        ProductInfo product = userInfoService.isProductType(productInfo.getProduct_title());
         if (product==null){
             int row = userInfoService.addProductType(productInfo);
             if (row > 0) {
                 httpResponse.setMsg("添加成功");
             } else {
                 httpResponse = new HttpResponse<>(StatusCode.Fail);
                 httpResponse.setMsg("添加失败");
             }
         }else {
             httpResponse = new HttpResponse<>(StatusCode.Fail);
             httpResponse.setMsg("该分类已存在，请勿重复添加");
         }

        httpResponse.setData(productInfo);
        return httpResponse;

    }


    /**
     * 修改商品分类
     */

    @RequestMapping(value = "editProductType", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<ProductInfo> editProductType(ProductInfo productInfo) {
        HttpResponse<ProductInfo> httpResponse = new HttpResponse<>(StatusCode.Success);
        int row = userInfoService.editProductType(productInfo);
        if (row > 0) {
            httpResponse.setMsg("修改成功");
        } else {
            httpResponse = new HttpResponse<>(StatusCode.Fail);
            httpResponse.setMsg("修改失败");
        }
        httpResponse.setData(productInfo);
        return httpResponse;

    }

    /**
     * 删除商品分类
     */
    @RequestMapping(value = "delProductTypeById", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<ProductInfo> delProductTypeById(int product_id) {
        HttpResponse<ProductInfo> httpResponse = new HttpResponse<>(StatusCode.Success);
        int row = userInfoService.delProductTypeById(product_id);
        if (row > 0) {
            httpResponse.setMsg("删除成功");
        } else {
            httpResponse.setMsg("删除失败");
        }
        httpResponse.setData(new ProductInfo());
        return httpResponse;

    }

    /**
     * 查询商品分类
     */
    @RequestMapping(value = "queryProductTypeList", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<ProductListInfo> queryProductTypeList() {
        HttpResponse<ProductListInfo> httpResponse = new HttpResponse<>(StatusCode.Success);
        httpResponse.setData(new ProductListInfo(userInfoService.queryProductTypeList()));
        return httpResponse;

    }

}
